<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>用户列表--CRM客户关系管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="resources/css/public.css" media="all"/>
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input searchVal" placeholder="请输入搜索用户姓名"/>
                </div>
                <a class="layui-btn search_btn" data-type="reload">搜索</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-normal addUsers_btn">添加用户</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
            </div>
        </form>
    </blockquote>
    <table id="userList" lay-filter="userList"></table>
    <!--操作-->
    <script type="text/html" id="userListBar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="look">查看</a>
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="assignRole">分配角色</a>
    </script>
</form>
<script type="text/javascript" src="resources/layui/layui.js"></script>
<script type="text/javascript" th:inline="none">
    layui.use(['form', 'layer', 'table', 'laytpl'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table;
        //用户列表
        var tableIns = table.render({
            elem: '#userList',
            url: 'users.do',
            cellMinWidth: 95,
            page: true,
            height: "full-200",
            limits: [10, 15, 20, 25],
            limit: 30,
            id: "userListTable",
            cols: [[
                {type: "checkbox", fixed: "left", width: 50},
                {field: 'telephone', title: '电话号码', minWidth: 100, align: "center"},
                {field: 'realname', title: '姓名', align: 'center'},
                {
                    field: 'birthday', title: '生日', align: 'center', minWidth: 90, templet: function (obj) {
                        return obj.birthday;
                    }
                },
                {
                    field: 'dept', title: '所属部门', align: 'center', minWidth: 100, templet: function (obj) {
                        return obj.dept.dname;
                    }
                },
                {
                    field: 'available', title: '用户状态', align: 'center', width: 120, templet: function (obj) {
                        return obj.available == 1 ? "启用" : "禁用";
                    }
                },
                {title: '操作', width: 260, templet: '#userListBar', fixed: "right", align: "center"}
            ]]
        });
        //实现搜索功能
        $(".search_btn").click(function () {
            var name = $(".searchVal").val();
            table.reload('userListTable', {url: "users.do?realname=" + name});
        });

        $(".addUsers_btn").click(function () {
            addUsers();
        })

        //添加用户
        function addUsers(edit) {
            var index = layui.layer.open({
                title: "添加用户",
                type: 2,
                content: "toUserPage.do"
            })
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize", function () {
                layui.layer.full(index);
            })
        }

        // tool(xx)这里的xx是和上边表个的lay-filter="userList"对应的
        table.on("tool(userList)", function (obj) {
            //获取当前行的数据
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    deleteUser(data);
                    break;
                case 'assignRole':
                    selectRole(data);
                    break;
                case 'edit':
                    editUser(data);
                    break;
            }
        });

        //删除方法
        function deleteUser(data) {
            layer.confirm('您确定要删除【' + data.telephone + '】这个用户吗？', {icon: 3, title: '提示'}, function (index) {
                $.ajax({
                    url: "user/deleteuser.do",
                    data: {id: data.id},
                    type: "get",
                    dataType: "json",
                    success: function (res) {
                        console.log(res + "****")
                        if (res.code == 200) {
                            //刷新表格
                            tableIns.reload();
                        } else if (res.code == -1) {
                            layer.msg(res.msg);
                        }
                    }
                });
                //关闭提示框
                layer.close(index);
            });
        }

        //批量删除
        $(".delAll_btn").click(function () {
            var checkStatus = table.checkStatus('userListTable');
            var data = checkStatus.data;
            var uIds = [];
            if (data.length > 0) {
                for (var i in data) {
                    uIds.push(data[i].id);
                }
                layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                    $.ajax({
                        url: "user/deleteBatchUser.do",
                        type: "GET",
                        data: {ids: uIds},
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 200) {
                                tableIns.reload();
                                layer.close(index);
                            }
                        }
                    });
                })
            } else {
                layer.msg("请选择需要删除的用户");
            }
        });

        //开发分配角色的弹出框
        function selectRole(data) {
            var assignRoleIndex = layer.open({
                type: 2,
                title: "分配角色",
                content: "toAssignRole.do",
                area: ['650px', '350px'],
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);//少了这个是不能从父页面向子页面传值的
                    body.contents().find("#userIdHiden").val(data.id);//这样就将值传递给子页面中id为userIdHiden的隐藏域
                }
            });
            window.sessionStorage.setItem("assignRoleIndex", assignRoleIndex);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize", function () {
                layui.layer.full(window.sessionStorage.getItem("index"));
            })
        }
    });

    //进入用户的编辑页面
    function editUser(data){
        var index=layui.layer.open({
            title:"编辑用户",
            type:2,
            content:"toUserEidtPage.do",
            success:function (layero,index) {
                var body = layui.layer.getChildFrame('body', index);//少了这个是不能从父页面向子页面传值的
                body.contents().find(".telephone").val(data.telephone);
                body.contents().find(".password").val(data.password);
                body.contents().find(".realname").val(data.realname);
                body.contents().find(".birthday").val(data.birthday);
                body.contents().find("#demo1").attr("src","showImage?imageFileName="+data.headimg);
                if(data.available==1){
                    body.contents().find("input[type='radio']").eq(0).attr("checked",true);
                }else if(data.available==0){
                    body.contents().find("input[type='radio']").eq(1).attr("checked",true);
                }
                var deptId=data.dept.id;
                var html;
                $.ajax({
                    url:'/depts/list.do',
                    success:function (data) {
                        html += "<option value=''>------------请选择------------</option>"
                        for (i = 0; i < data.length; i++) {
                            if(deptId==data[i].id){
                                html += ("<option value='" + data[i].id + "' selected>" + data[i].dname + "</option>")
                            }else{
                                html += ("<option value='" + data[i].id + "'>" + data[i].dname + "</option>")
                            }

                        }
                        body.contents().find("#userDept").append(html);
                        form.render('select');
                    }
                });

            }
        });
        layui.layer.full(index);//全屏显示
    }
</script>
</body>
</html>